package com.rzico.report.controller;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.rzico.base.BaseController;
import com.rzico.base.CommResult;
import com.rzico.basics.entity.Enterprise;
import com.rzico.basics.service.EnterpriseService;
import com.rzico.entity.PageResult;
import com.rzico.entity.Pageable;
import com.rzico.report.entity.OrderSaleCountVo;
import com.rzico.report.entity.ReceiveCountVo;
import com.rzico.report.service.OrderSaleCountService;
import com.rzico.report.service.ReceiveCountService;
import com.rzico.util.StringUtils;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.apache.commons.lang3.time.DateUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.*;

/**
 * 收支统计报表
 *
 * @author Rzico Boot
 * @version 1.0
 * @date 2020-11-15
 */
@Api(description = "收支统计报表")
@RestController
@RequestMapping("/report/receiveReport")
public class ReceiveReportController extends BaseController{

    @Autowired
    private ReceiveCountService receiveCountService;

    @Autowired
    private EnterpriseService enterpriseService;

    /**
     * 按日期分组统计
     *
     * @return
     */
    @ApiOperation("按日期分组统计")
    @PostMapping("/dateList")
    @ApiImplicitParams({@ApiImplicitParam(name = "startDate", value = "开始时间", dataType = "String", paramType = "query") ,
            @ApiImplicitParam(name = "endDate", value = "结束时间", dataType = "String", paramType = "query"),
            @ApiImplicitParam(name = "shopId", value = "门店ID", dataType = "Long", paramType = "query"),
            @ApiImplicitParam(name = "employeeId", value = "收款人ID", dataType = "String", paramType = "query")
    })
    public CommResult<ReceiveCountVo> dateList(String productCategoryId, Long shopId,String employeeId, String officeId,
                                               Date startDate, Date endDate, Pageable pageable) {
        Enterprise enterprise = enterpriseService.getCurrent();
        Map<String, Object> params = new HashMap<String, Object>();
        if (startDate!=null){
            params.put("startDate", DateUtils.truncate(startDate, Calendar.DATE));
        }
        if (endDate!=null){
            params.put("endDate", DateUtils.addMilliseconds(DateUtils.addDays(endDate,1),-1));
        }
        if (null != shopId){
            params.put("shopId", shopId);
        }
        if (null != employeeId){
            params.put("employeeId", employeeId);
        }
        params.put("enterpriseId", enterprise.getId());
        Page<Object> startPage = PageHelper.startPage(pageable.getPageNum(), pageable.getPageSize());
        List<ReceiveCountVo> list = receiveCountService.selectDateList(params);
        PageResult<ReceiveCountVo> pageResult = new PageResult<ReceiveCountVo>(list, startPage.getTotal(), pageable);
        return CommResult.success(pageResult);
    }

    /**
     * 底部汇总合计
     *
     * @return
     */
    @ApiOperation("底部汇总合计")
    @PostMapping("/summary")
    @ApiImplicitParams({@ApiImplicitParam(name = "startDate", value = "开始时间", dataType = "String", paramType = "query") ,
            @ApiImplicitParam(name = "endDate", value = "结束时间", dataType = "String", paramType = "query"),
            @ApiImplicitParam(name = "shopId", value = "门店ID", dataType = "Long", paramType = "query"),
            @ApiImplicitParam(name = "employeeId", value = "收款人ID", dataType = "String", paramType = "query")
    })
    public CommResult<ReceiveCountVo> summary(String productCategoryId, Long shopId, String employeeId, String officeId,
                                              Date startDate, Date endDate, Pageable pageable) {
        Enterprise enterprise = enterpriseService.getCurrent();
        Map<String, Object> params = new HashMap<String, Object>();
        if (startDate!=null){
            params.put("startDate",DateUtils.truncate(startDate, Calendar.DATE));
        }
        if (endDate!=null){
            params.put("endDate", DateUtils.addMilliseconds(DateUtils.addDays(endDate,1),-1));
        }
        if (null != shopId){
            params.put("shopId", shopId);
        }
        if (null != employeeId){
            params.put("employeeId", employeeId);
        }
        params.put("enterpriseId", enterprise.getId());
        List<ReceiveCountVo> list = receiveCountService.selectSummary(params);
        return CommResult.success(list);
    }

}
